package com.ikun.yummy.sourceop.op;
import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.ikun.yummy.bean.CommentInfo;
import com.ikun.yummy.bean.CookInfo;
import com.ikun.yummy.properties.AppProperties;
import com.ikun.yummy.sourceop.sqlutils.SqliteHelper;

import java.util.ArrayList;
import java.util.List;
public class CommentOP {
    private final SqliteHelper helper;
    private final SQLiteDatabase db;
    public CommentOP(Context context){
        helper = new SqliteHelper(context);
        db = helper.getReadableDatabase();
    }
    public void addComment(CommentInfo commentInfo){
        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("username", commentInfo.username);
        values.put("content", commentInfo.content);
        values.put("reply", commentInfo.reply);
        values.put("cookid", commentInfo.cookid);
        db.insert(AppProperties.COMMENT_TABLE, null, values);
        db.setTransactionSuccessful();
        db.endTransaction();
    }
    public List<CommentInfo> queryByCookid(String cookid){
        String sql="SELECT * FROM "+AppProperties.COMMENT_TABLE+" WHERE cookid="+cookid;
        @SuppressLint("Recycle") Cursor cursor =db.rawQuery(sql,null,null);
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<CommentInfo> list = new ArrayList<>();
            while (cursor.moveToNext()){
                list.add(new CommentInfo(1, cursor.getString(1),cursor.getString(2),cursor.getString(3),"",cursor.getString(4),cursor.getString(0)));
            }
            return list;
        }else{
            return null;
        }
    }
    public void deleteCommentByCommentId(String commentid){
        db.delete(AppProperties.COMMENT_TABLE, "commentid=?",new String[]{commentid});
    }
    public CommentInfo queryCommentByCommentId(String commentid){//长按回复时用来删除回复所要用到的回复info，CommentInfo不是评论是回复
        @SuppressLint("Recycle") Cursor cursor = db.rawQuery("SELECT * FROM " + AppProperties.COMMENT_TABLE + " WHERE commentid=" + commentid, null, null);
        if (cursor!=null&&cursor.getCount()>0){
            while (cursor.moveToNext()){
                return new CommentInfo(1,
                        cursor.getString(1),
                        cursor.getString(2),
                        cursor.getString(3),
                        "",
                        cursor.getString(4),
                        cursor.getString(0));
            }
            return null;
        }else{
            return null;
        }
    }
    public void updateByCommentId(CommentInfo commentInfo) {
        ContentValues values = new ContentValues();
        if (commentInfo.username!=null){
            values.put("username", commentInfo.username);
        }
        if (commentInfo.reply!=null){
            values.put("reply", commentInfo.reply);
        }
        if (commentInfo.content!=null){
            values.put("content", commentInfo.content);
        }
        if (commentInfo.cookid!=null){
            values.put("cookid", commentInfo.cookid);
        }
        db.update(AppProperties.COMMENT_TABLE,values, "commentid=?" ,new String[]{commentInfo.commentid});
    }
    public void deleteCommentCookid(String cookid){//吧cook的所有评论删掉
        db.delete(AppProperties.COMMENT_TABLE, "cookid=?",new String[]{cookid});
    }
}